package com.situ.mall.controller;

import com.situ.mall.pojo.Admin;
import com.situ.mall.service.IAdminService;
import com.situ.mall.util.Result;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private IAdminService adminService;

    @RequestMapping("/login")
    public Result login(String name, String password, String code, HttpSession session) {
        System.out.println("AdminController.login");
        String codeInSession = (String) session.getAttribute("codeInSession");
        if (!codeInSession.equalsIgnoreCase(code)) {
            return Result.error("验证码错误");
        }

        Admin admin = adminService.login(name, password);
        if (admin == null) {
            return Result.error("用户名或密码错误");
        }

        session.setAttribute("admin", admin);
        return Result.ok("登录成功");
    }

    @RequestMapping("/logout")
    public Result logout(HttpSession session) {
        session.removeAttribute("admin");
        return Result.ok("退出成功");
    }
}
